layui.use(['form', 'table'], function () {
    let $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#tbl',
        url: ctx +'/api/admin_users',
        toolbar: '#toolbar',
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'id', width: 80, title: '主键', sort: true},
            {field: 'username', width: 120, title: '用户名',sort:true},
            {field: 'password', width: 180, title: '密码'},
            {field: 'avatarUrl', width: 200, title: '头像'},
            {field: 'description', width:280,title: '备注'},
            {title: '操作', minWidth: 150, toolbar: '#row-toolbar', align: "center"}
        ]],
        limits: [10, 20, 30, 50, 100],
        limit: 10,
        page: true,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(search-btn)', function (data) {

        //表格重载
        table.reload('tbl', {
            page: {
                curr: 1
            }, where: data.field
        }, 'data');

        return false;
    });
    $("#reset-btn").click(function (){
        $("#search-form")[0].reset();
    })
    /**
     * toolbar监听事件
     */
    table.on('toolbar(tbl)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
             layer.open({
                title: '添加后台用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['800px', '500px'],
                content: ctx + '/admin_user/add',
            });
        } else if (obj.event === 'delete') {  // 监听批量删除操作
            let checkStatus = table.checkStatus('tbl')
                , data = checkStatus.data;
            let ids = [];
            data.forEach(t => ids.push(t.id));
            if (ids.length === 0){
                layer.msg("请选中您要删除的记录")
            }else{
                layer.confirm("是否确认删除选中的记录",function(index){
                    deleteByIds(ids);
                    table.reload("tbl");
                    layer.close(index);
                })
            }
        }
    });

    //监听表格复选框选择
    table.on('checkbox(tbl)', function (obj) {
        console.log(obj)
    });
    //监听表格行工具栏
    table.on('tool(tbl)', function (obj) {
        let data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                title: '修改后台用户',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['800px', '500px'],
                content: ctx + '/admin_user/edit?id=' + data.id,
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('是否确认删除', function (index) {
                let ids = [obj.data.id];
                deleteByIds(ids)
                table.reload("tbl");
                layer.close(index);
            });
        }
    });

    function deleteByIds(ids){
        let url = ctx + "/api/admin_users"
        $.ajax({
            url,
            method:"delete",
            dataType:"json",
            data:{
                ids
            },
            traditional:true,
            success(resp){
                if (resp.success){
                    layer.msg("成功删除"+resp.rows+"条数据");
                }else{
                    layer.alert(resp.error);
                }
            }
        });
    }

});